package com.superdry.activity;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ExpandableListView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ab.activity.AbActivity;
import com.ab.http.AbHttpUtil;
import com.ab.http.AbRequestParams;
import com.ab.http.AbStringHttpResponseListener;
import com.alibaba.fastjson.JSONObject;
import com.drysurper.R;
import com.superdry.adapter.ExpandableListViewAdapter;
import com.superdry.json.Address;
import com.superdry.json.Clothes;
import com.superdry.json.Userinfo;
import com.superdry.utils.GlobalConst;
import com.superdry.utils.SPHelper;
import com.superdry.utils.StringUtils;

public class EnsureChoiceOfClothActivity extends AbActivity implements OnClickListener {

	private ExpandableListView expandableListView;
	private ExpandableListViewAdapter treeViewAdapter;

	private TextView total_cloth_count;
	private TextView total_money;
	private TextView ensure_cloth;

	private Button ensure_choice;
	private LinearLayout choose_cloth_goback;
	private AbHttpUtil mAbHttpUtil = null;
	private List<String> groups = new ArrayList<String>();
	private List<Clothes> cloths;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// 获取Http工具类
		mAbHttpUtil = AbHttpUtil.getInstance(this);
		mAbHttpUtil.setTimeout(10000);
		setContentView(R.layout.aty_ensure_choice_cloth);
		initCloths();
		initViews();
	}
	

	/**
	 * 初始化衣服的类型
	 */
	private void initCloths() {
		cloths = new ArrayList<Clothes>();
		Clothes c1 = new Clothes(9, "T恤,衬衫,背心,马甲,长裤,短裤,短裙,内衣,内裤,睡裙,睡裤,帽子,领带,围巾,床单,枕套");
		Clothes c2 = new Clothes(19, "西装,体闲外套,牛仔衣,毛衣,针织衫,真丝上衣,亚麻上衣,连衣裙,棉马甲,羽绒马甲背心,羊毛马甲背心,羊绒羊毛衫,毛领,披肩,被套");
		Clothes c3 = new Clothes(29, "风衣,大衣,冲锋衣,羽绒衣,棉衣,睡袍,礼服,毛毯,床上四件套");
		cloths.add(c1);
		cloths.add(c2);
		cloths.add(c3);
	}

	/**
	 * 初始化组件
	 */
	private void initViews() {

		total_cloth_count = (TextView) findViewById(R.id.total_cloth_count);
		total_money = (TextView) findViewById(R.id.total_money);
		ensure_cloth = (TextView) findViewById(R.id.ensure_cloth);

		ensure_choice = (Button) findViewById(R.id.ensure_choice);
		choose_cloth_goback = (LinearLayout) findViewById(R.id.choose_cloth_goback);

		total_cloth_count.setText(getIntent().getStringExtra("total_count"));
		total_money.setText(getIntent().getStringExtra("total_money"));

		ensure_choice.setOnClickListener(this);
		choose_cloth_goback.setOnClickListener(this);
		ensure_cloth.setOnClickListener(this);
		setListView(cloths);
		/*AbRequestParams params = new AbRequestParams();
		mAbHttpUtil.post(GlobalConst.GET_CLOTHES_TYPE, params, new AbStringHttpResponseListener() {

			@Override
			public void onStart() {
				System.out.println();
			}

			@Override
			public void onFinish() {
				System.out.println();
			}

			@Override
			public void onFailure(int statusCode, String content, Throwable error) {
				System.out.println();
			}

			@Override
			public void onSuccess(int statusCode, String content) {
				List<Clothes> cloths = JSONObject.parseArray(content, Clothes.class);
				// 进行价格排序
				Collections.sort(cloths, new Comparator<Clothes>() {

					@Override
					public int compare(Clothes c1, Clothes c2) {
						return (int)c1.getPrice() - (int)c2.getPrice();
					}
				});
				setListView(cloths);
			}
		});*/
	}

	private void setListView(List<Clothes> cloths) {
		treeViewAdapter = new ExpandableListViewAdapter(this, ExpandableListViewAdapter.PaddingLeft >> 1, total_cloth_count, total_money);
		expandableListView = (ExpandableListView) findViewById(R.id.expandableListView);
		expandableListView.setGroupIndicator(null);
		List<ExpandableListViewAdapter.TreeNode> treeNode = treeViewAdapter.GetTreeNode();
		for (Clothes cloth : cloths) {
			ExpandableListViewAdapter.TreeNode node = new ExpandableListViewAdapter.TreeNode();
			node.parent = cloth.getPrice() + "";
			node.childs = cloth;
			treeNode.add(node);
		}
		treeViewAdapter.UpdateTreeNode(treeNode);
		expandableListView.setAdapter(treeViewAdapter);

	}

	@Override
	public void onClick(View view) {

		switch (view.getId()) {
		case R.id.choose_cloth_goback:
			finish();
			break;
		case R.id.ensure_choice:
			isAddressExist();
			break;
		case R.id.ensure_cloth:
			isAddressExist();
			break;

		default:
			break;
		}
	}

	/**
	 * 根据服务器端是否有地址决定跳到的activity
	 */
	private void isAddressExist() {
		AbRequestParams params = new AbRequestParams();
		// 得到用户的id
//		String userId = "16";
//		params.put("userID", userId);
		Userinfo info = SPHelper.getUserInfo(EnsureChoiceOfClothActivity.this);
		params.put("userID", info.getUserID()+"");
		mAbHttpUtil.post(GlobalConst.GET_ADDRESS_EXIST, params, new AbStringHttpResponseListener() {

			@Override
			public void onStart() {
			}

			@Override
			public void onFinish() {
			}

			@Override
			public void onFailure(int statusCode, String content, Throwable error) {
			}

			@Override
			public void onSuccess(int statusCode, String content) {
				List<Address> addresses = JSONObject.parseArray(content, Address.class);
				if (addresses == null || addresses.size() == 0 || StringUtils.isEmpty(addresses.get(0).getAddressName())) {
					// 没有地址
					toAddAddress();
				} else {
					// 有地址 默认显示第一个
					toPlaceOrder(addresses.get(0));
				}
			}
		});

	}
	private void toAddAddress() {
		Intent intent = new Intent(this, Add_AddressActivity.class);
		startActivity(intent);
	}

	/**
	 * 跳转到确认订单页面
	 */
	private void toPlaceOrder(Address address) {
		Intent intent = new Intent(this, PlaceOrderActivity.class);
		intent.putExtra("address", address);
		startActivity(intent);
	}
}
